/*
 * @lc app=leetcode.cn id=977 lang=swift
 *
 * [977] 有序数组的平方
 */

// @lc code=start
class Solution {
    func sortedSquares(_ nums: [Int]) -> [Int] {
        var left = 0, right = nums.count - 1
        var index = right
        var arr = nums
        while left <= right {
            let num1 = nums[left] * nums[left]
            let num2 = nums[right] * nums[right]
            if num1 > num2 {
                arr[index] = num1
                left += 1
            } else {
                arr[index] = num2
                right -= 1
            }
            index -= 1
        }
        return arr
    }
}
// @lc code=end

